import global from "../../global";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Client extends cc.Component {

    static arr_data:Array<any> = new Array();       //对象没创建

    onLoad(){
        global.client = this;
        //浏览器切后台 || 退出游戏
        cc.game.on(cc.game.EVENT_HIDE, ()=>{
            //二人局直接结束，三人局判断对局存活玩家
            this.send({method:"dropLine",id:global.id});
        });
    }

    start(){
        this.gogogo();
    }

    onDisable(){
        cc.game.off(cc.game.EVENT_HIDE);
        cc.game.off(cc.game.EVENT_SHOW);
    }

    // SDK 发送房间消息
    send(obj:Object) {
        //这是复盘操作，不用发出去
        if(global.is_replay){
            return;
        }
        //global.websocket.readyState) == 1 连接成功  3  断开
        global.websocket.send(JSON.stringify(obj))
    }

    /**
     * 从后台切回来了
     */
    gogogo(){
        if(Client.arr_data.length==0){
            return;
        }else{
            this.onMessage(Client.arr_data.shift())
            this.gogogo(); 
        }
    }

    /**
     * 战斗房间的消息
     */
    onMessage(data:any){
        global.mgr.receive(data);        
    }

}
